/*
* 密码框
 */

$("#paypwd").on("input propertychange", function () {
    showPaypwd(true);
})

function showPaypwd(focus) {
    var pwd = $("#paypwd").val();

    $(".paypwd li").each(function (index, elem) {
        if (index < pwd.length) {
            $(this).text("•");
            $(this).removeClass("b");
        } else if (focus && index == pwd.length) {
            $(this).text("");
            $(this).addClass("b");
        } else {
            $(this).text("");
            $(this).removeClass("b");
        }
    });
}

$("#paypwd").focus(function () {
    $(".paypwd li").css({
        "border-color": "#FF6700"
    });
    showPaypwd(true);
})

$("#paypwd").blur(function () {
    $(".paypwd li").css({
        "border-color": "#E0E0E0"
    });

    showPaypwd(false);
})

$("#pay").submit(function () {
    var payPassword = $("#paypwd").val();
    var orderId = $("input[name='id']").val();
    if (payPassword.length < 6) {
        alert("请输入6位支付密码!")
        return false;
    }
//ajax支付订单，订单ID，支付密码
    obajax.put(
        '/api/user/order/pay',
        {orderId, payPassword},
        (result) => {
            //跳转订单列表，我的订单
            alert("支付成功");
            window.location.href='/user/order/ucOrder';
        },
        (result) => {
            alert(result.msg);
            //刷新当前页面
            location.reload();
        }
    )
    return false;
})

// 加载订单的信息
function loadOrder(id) {
    obajax.get(
        '/api/user/order/' + id,
        {},
        (result) => {
            if (result.data.status != 0){
                window.location.href='/user/order?id='+id;
            }
            //显示
            $('.order1').text(result.data.id);
            $('.addr').text(result.data.addrDetail);
            //计算订单的总额
            var sum = 0;
            $.each(result.data.details, (index, detail) => {
                sum += detail.goods.price * detail.count;
            })
            $('.prices').text(sum);
        },
        (result) => {
            alert(result.msg);
            window.location.href = "/";
        }
    )
}